package com.baidu.location.demo;

import static com.util.util.setSize;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.entity.Authority;
import com.entity.Login;
import com.entity.Response;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.util.DialogUtil;
import com.util.FileUtil;
import com.util.HttpUtils;

import org.json.JSONException;

import java.io.IOException;

public class LoginActivity extends AppCompatActivity {
    public static boolean isCreate=false;
    private EditText user;
    private EditText pass;
    private Button loginBut;
    private Button registerBut;
    private LinearLayout loginView;
    private Display display;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        isCreate=true;
        display = getWindowManager().getDefaultDisplay();
        user=findViewById(R.id.user);
        pass=findViewById(R.id.pass);
        loginBut=findViewById(R.id.loginBut);
        registerBut=findViewById(R.id.registerBut);
        registerBut.setOnClickListener(view -> startActivity(new Intent(LoginActivity.this,CreateuserActivity.class)));
        loginView=findViewById(R.id.loginView);
        setSize(loginView,display.getWidth()/2,0);
        loginBut.setOnClickListener(view -> {
            if(user.getText().toString().isEmpty()||pass.getText().toString().isEmpty()){
                DialogUtil.CreateDoalog(LoginActivity.this,"账号或者密码不能为空",0, QMUITipDialog.Builder.ICON_TYPE_FAIL);
                return;
            }
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    Response<String> encrypt=JSONObject.parseObject(HttpUtils.Get("Util/MD5Encrypt?data="+pass.getText()),new TypeReference<Response<String>>(){});
                    Response<Login> data= JSONObject.parseObject(HttpUtils.Post("Login/Login?user="+user.getText()+"&pass="+encrypt.getValue(),""),new TypeReference<Response<Login>>(){});
                    if(data.getStatusCode()==200){
                        Authority.setAuthorization(data.getValue().getToken());
                        try {
                            FileUtil.Save(LoginActivity.this,"user.json",JSONObject.toJSONString(data.getValue().getUser()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Intent i=new Intent(LoginActivity.this,MainActivity.class);
                        i.putExtra("user", JSON.toJSONString(data.getValue().getUser()));
                        (LoginActivity.this).runOnUiThread(() -> {
                            startActivity(i);
                            LoginActivity.this.finish();
                        });
                    }else{
                        LoginActivity.this.runOnUiThread(() -> Toast.makeText(LoginActivity.this, data.getMessage(),Toast.LENGTH_LONG).show());
                    }

                }
            }.start();
        });
    }
}
